Taming Macros
نویسندگان
چکیده
Scheme includes a simple yet powerful macro mechanism. Using macros, programmers can easily extend the language with new kinds of expressions and definitions, thus abstracting over recurring syntactic patterns. As with every other powerful language mechanism, programmers can also easily misuse macros and, to this day, broken macro definitions or macro uses pose complex debugging problems to programmers at all levels of experience. In this paper, we present a type system for taming Scheme-like macros. Specifically, we formulate a small model that captures the essential properties of Scheme-style macros. For this model, we formulate a novel type system to eliminate some of these problems, prove its soundness, and validate its pragmatic usefulness. 1 The Power of Macro Programming Over the past 20 years, the Scheme community has developed an expressive and easy-to-use standard macro system [1]. To introduce a new construct via a macro, a programmer simply writes down a rewriting rule consisting of a pair of syntactic patterns [2]. The left-hand side is called a pattern; the right-hand side is referred to as a template. Collectively the set of rules specifies how the macro expander must translate the surface syntax into core syntax. The macro expander eliminates each instance of the pattern in the program by replacing it with an instance of the template where all pattern variables have been appropriately substituted. Scheme implementors often use the macro system to equip the language with additional forms of expressions. Programmers use the macro system for similar reasons. Many define small domain-specific notations and then formulate their program in a mix of Scheme and domain-specific expressions [3, 4]. The macro system thus empowers them to follow the old Lisp maxim on problem-solving via language definition, which says that programmers should formulate an embedded programming language for the problem domain and that they should express their solution for the domain in this new language. Naturally, every powerful programming construct invites misuse. For example, a programmer may pass the wrong number of arguments to a function or attempt to apply an operation on the wrong kind of value (such as destructuring a number as if it were a pair or dividing by zero). These errors may cause incorrect program results or run-time exceptions. A programmer may misuse macros in similar ways. Misuse of macros may cause the generation of an incorrect program or the triggering of errors during compilation. Given the weak programming environments for macro expansion, debugging macro misuses is extremely difficult compared to other constructs. With this paper, we attempt to reconcile type checking with powerful syntactic abstractions in the spirit of Scheme. Naturally, the type system reduces the power of the macro system, but we believe that it retains enough for Scheme programmers with ordinary problems. We discuss the one exception to this claim in the conclusion. In the meantime, we briefly introduce macros; illustrate potential misuses; and present our unusual type system and a model for macro expansion so that we can prove type soundness. The penultimate section shows how we can extend our model to accommodate most of Scheme’s expressive powers. Finally, we discuss related and future work toward taming macros. Note: A preliminary version of this paper appeared in the Scheme and Functional Programming Workshop 2003 [5].
منابع مشابه
Well-Shaped Macros
Scheme includes an easy-to-use and powerful macro mechanism for extending the programming language with new expression and definition forms. Using macros, a Scheme programmer can define a new notation for a specific problem domain and can then state algorithms in this language. Thus, Scheme programmers can formulate layers of abstraction whose expressive power greatly surpasses that of ordinary...
متن کاملT-EDIT, A Collection of Editing Macros for TEX
T-EDIT is a powerful collection of editing macros designed specifically for TEX. The macros are designed to be used with the KEDIT editor on IBM PC or PC-compatible computers. The user is aided in every step of the preparation of a completed document. Menus or prompting messages are included with many of the macros. Over 1250 TEX and AMS-TEX control sequences may be accessed through menus. The ...
متن کاملFirst as Farce, Then as Filmfarsi: Film Adaptation of Shakespeare’s The Taming of the Shrew in Iran
This article is concerned with William Shakespeare’s famous farce play The Taming of the Shrew and its Persian adaptation as an Iranian film called Gorbe ra dame Hejleh Mikoshand in 1969. The point that informs the inquiry is the way the film departs and differs from the play in relation to the issue of women within the patriarchal society. The play and the film will be examined separately in d...
متن کاملMacros vs. scripting in VPOET
We present our experience on the provision and use of macros for the management of semantic data in semantically-enabled web applications. Macros can be considered as a lightweight version of scripting languages, mostly oriented to end users instead of to developers. We have enabled the use of macros in a wiki-based application named VPOET, oriented to web designers, and have confirmed through ...
متن کاملAssessment of Yield Loss in Rice due to Yellow Stem Borer, Scirpophaga incertulas, Using Simulation Models
The yield loss simulation (YLS) and the MACROS LIQ.CSM models were used to simulate the effect of stem borer damage at a detillering rate of 5, 15, 30, 60% at vegetative, panicle initiation and grain filing stages, on yield of rice cultivar?s Ratna. This stem borer damage during vegetative and panicle initiation stages was not simulated appropriately by both the models. Relatively MACROS simul...
متن کامل